package com.lvs.zmwz.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lvs.zmwz.comm.DictionaryEnum;
import com.lvs.zmwz.entity.Shop;
import com.lvs.zmwz.entity.ShopType;
import com.lvs.zmwz.service.IShopService;
import com.lvs.zmwz.service.IShopTypeService;
import com.lvs.zmwz.util.IdentityUtil;
import com.lvs.zmwz.util.QPage;
import com.lvs.zmwz.util.ResultView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商品分类表 前端控制器
 * </p>
 *
 * @author 
 * @since 2022-07-22
 */
@RestController
@RequestMapping("/api/admin/shop-type")
public class ShopTypeController {


    @Autowired
    private IShopTypeService iShopTypeService;

    @Autowired
    private IShopService iShopService;

    /**
     * 分类列表
     * @return
     */
    @GetMapping("/list")
    public ResultView list(QPage qPage, ShopType sysManagerSearch) {
        IPage<ShopType> hjRoleList=this.iShopTypeService.getListAll(qPage,sysManagerSearch);
        return ResultView.ok(hjRoleList);
    }


    /**
     * 分类列表-不分页
     * @return
     */
    @GetMapping("/select")
    public ResultView select() {
        QueryWrapper<ShopType> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("is_del",DictionaryEnum.IS_DEL_N.getCode());
        List<ShopType> shopTypes=this.iShopTypeService.list(queryWrapper);
        return ResultView.ok(shopTypes);
    }


    /**
     * 添加分类
     * @return
     */
    @PostMapping("/add")
    public ResultView add(ShopType shop) {
        shop.setTypeId(IdentityUtil.identityId("TP"));
        shop.setCtime(new Date());
        shop.setIsDel(DictionaryEnum.IS_DEL_N.getCode());
        boolean isOk=this.iShopTypeService.save(shop);
        if(isOk){
            return ResultView.ok();
        }else{
            return ResultView.error("新增分类失败");
        }
    }


    /**
     * 添加分类
     * @return
     */
    @GetMapping("/getId")
    public ResultView getId(String typeId) {
        return ResultView.ok(this.iShopTypeService.getById(typeId));
    }

    /**
     * 修改分类
     * @return
     */
    @PostMapping("/update")
    public ResultView update(ShopType shop) {
        boolean isOk=this.iShopTypeService.updateById(shop);
        if(isOk){
            return ResultView.ok();
        }else{
            return ResultView.error("修改分类失败");
        }
    }


    /**
     * 删除分类
     * @return
     */
    @PostMapping("/delete")
    public ResultView delete(String typeId) {
        //验证当前分类是否绑定了商品
        QueryWrapper<Shop> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("is_del",DictionaryEnum.IS_DEL_N.getCode());
        queryWrapper.eq("type_id",typeId);
        List<Shop> shopList=this.iShopService.list(queryWrapper);
        if(shopList!=null&&shopList.size()>=1){
            return ResultView.error("当前分类还存在商品，不能删除");
        }
        ShopType shop=this.iShopTypeService.getById(typeId);
        shop.setIsDel(DictionaryEnum.IS_DEL_Y.getCode());
        boolean isOk=this.iShopTypeService.updateById(shop);
        if(isOk){
            return ResultView.ok();
        }else{
            return ResultView.error("删除失败");
        }
    }

}
